﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Nest;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //var node = new Uri("http://localhost:9200");
            //var settings = new ConnectionSettings(node).DefaultFieldNameInferrer((name) => name);
            //var client = new ElasticClient(settings);

            //client.Index(new TestModel5
            //{
            //    Id = 1000,
            //    Name = "liwei",
            //    Dic = "lw",
            //    State = 1,
            //    Deleted = false,
            //    AddTime = DateTime.Now,
            //    PassingRate = 105,
            //    Dvalue = 23.88
            //}, s => s.Index("test-index3").Type("TestModel5"));

            var mm = new DateTime(DateTime.Now.Year, DateTime.Now.Month-1, 30);
            MessageBox.Show(mm.ToString("yyyy-MM-dd"));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                var node = new Uri("http://localhost:9200");
                var settings = new ConnectionSettings(node).DefaultIndex("test-index3").DefaultFieldNameInferrer((name) => name);
                var client = new ElasticClient(settings);

                //var datas = new List<TestModel5>();
                //for (int i = 0; i < 10; i++)
                //{
                //    datas.Add(new TestModel5
                //    {
                //        Id = 1001 + i,
                //        Name = "liwei",
                //        Dic = "lw",
                //        State = 1,
                //        Deleted = false,
                //        AddTime = DateTime.Now,
                //        PassingRate = 105,
                //        Dvalue = 23.88
                //    });
                //}

                //var t =client.IndexMany<TestModel5>(datas, "test-index3", typeof(TestModel5));

                var t = client.Index(new TestModel5
                {
                    Id = 100013,
                    Name = "liwei2",
                    Dic = "lw1",
                    State = 1,
                    Deleted = false,
                    AddTime = DateTime.Now,
                    PassingRate = 2005,
                    Dvalue = 123.88
                }, s => s.Index("test-index3").Type("TestModel"));

                MessageBox.Show(t.ToString());
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                throw;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var listTest = new List<TestModel5>();

            TestModel5 mt = new TestModel5();
            mt.Deleted = false;
            mt.AddTime=DateTime.Now;
            mt.Id = 1;

            listTest.Add(mt);
            listTest.Add(mt);
            listTest.Add(mt);
            listTest.Add(mt);

            listTest.Sort((x, y) =>
            {
                if (x.Id < y.Id)
                    return -1;
                else
                {
                    return 0;
                }
            });

            return;
            var node = new Uri("http://localhost:9200");
            var settings = new ConnectionSettings(node).DefaultIndex("test-index3");//.DefaultFieldNameInferrer((name) => name);
            settings.EnableTcpKeepAlive(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(2)).DisableDirectStreaming().PrettyJson();
            var client = new ElasticClient(settings);

            var searchResponse = client.Search<TestModel5>(s => s
                .From(0)
                .Size(10)
                .Query(q => q
                    .Match(m => m
                        .Field(f => f.Name)
                        .Query("liwei")
                    )
                )
            );

            var rst = searchResponse.Documents;
        }
    }

    [ElasticsearchType(Name = "TestModel", IdProperty = "Id")]
    public class TestModel5
    {
        [Number(NumberType.Long, Name = "Id")]
        public long Id { get; set; }
        /// <summary>
        /// keyword 不分词
        /// </summary>
        [Keyword(Name = "Name", Index = true)]
        public string Name { get; set; }
        /// <summary>
        /// text 分词,Analyzer = "ik_max_word"
        /// </summary>
        [Text(Name = "Dic", Index = true,Analyzer = "ik_max_word")]
        public string Dic { get; set; }

        [Number(NumberType.Integer, Name = "State")]
        public int State { get; set; }

        [Boolean(Name = "Deleted")]
        public bool Deleted { get; set; }
        [Date(Name = "AddTime")]
        public DateTime AddTime { get; set; }

        [Number(NumberType.Float, Name = "PassingRate")]
        public float PassingRate { get; set; }

        [Number(NumberType.Double, Name = "Dvalue")]
        public double Dvalue { get; set; }
    }
}
